package com.huizhonglm.diap.widget.xGallery.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.image.OnLoadImageListener;
import com.huizhonglm.diap.core.image.PreviewLoader;
import com.huizhonglm.diap.core.image.Thumb;
import com.huizhonglm.diap.widget.CircleProgressBar;


public class XTouchView extends RelativeLayout {

    protected CircleProgressBar circleProgressBar;
//    protected MySmallProgressBar mProgressBar;
    protected XImageView mImageView;
    protected Context mContext;

    public XTouchView(Context ctx) {
        super(ctx);
        mContext = ctx;
        init();
    }

    public XTouchView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
        mContext = ctx;
        init();
    }

    public XImageView getImageView() {
        return mImageView;
    }

    protected void init() {
        mImageView = new XImageView(mContext);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mImageView.setLayoutParams(params);
        this.addView(mImageView);
        mImageView.setVisibility(GONE);

//        mProgressBar = new MySmallProgressBar(mContext);
//        params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
//        params.addRule(RelativeLayout.CENTER_VERTICAL);
//        mProgressBar.setLayoutParams(params);
//        this.addView(mProgressBar);

        circleProgressBar = new CircleProgressBar(mContext);
        params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        circleProgressBar.setLayoutParams(params);
        this.addView(circleProgressBar);
    }

    /**
     * 加载本地图片
     *
     * @param imgPath 图片路径
     * @param ccbk    点击回调函数
     */
    public void setPath(String imgPath, final OnClickCallBack ccbk) {
        circleProgressBar.setVisibility(GONE);
        mImageView.setTag(imgPath);
        mImageView.setOnClickImageListener(new XImageView.OnClickImageListener() {
            @Override
            public void onClickImage() {
                if (ccbk != null) {
                    ccbk.onClickCallBack();
                }
            }
        });
        mImageView.setScaleType(ScaleType.MATRIX);
        if (EmptyUtil.isEmpty(imgPath)) {
            mImageView.setScaleType(ScaleType.CENTER);
            mImageView.setImageResource(R.drawable.def_logo);
        } else {
            String path = imgPath;
            if (!imgPath.contains("file://")) {
                path = "file://" + imgPath;
            }
            Thumb.getInstance().display(mImageView, path, R.drawable.def_logo);
        }
        mImageView.setVisibility(VISIBLE);
    }

    /**
     * 加载网络图片
     *
     * @param imgUrl 图片地址
     * @param scbk   保存图片回调函数
     * @param ccbk   点击图片回调函数
     */
    public void setUrl(String imgUrl, final OnSaveCallBack scbk, final OnClickCallBack ccbk) {
        mImageView.setTag(imgUrl);
        mImageView.setOnSaveImageListener(new XImageView.OnSaveImageListener() {
            @Override
            public void onSaveImage(Bitmap bmp) {
                if (scbk != null) {
                    scbk.onSaveCallBack(bmp);
                }
            }
        });
        mImageView.setOnClickImageListener(new XImageView.OnClickImageListener() {
            @Override
            public void onClickImage() {
                if (ccbk != null) {
                    ccbk.onClickCallBack();
                }
            }
        });
        PreviewLoader.getInstance().displaySync(imgUrl, new OnLoadImageListener() {
            @Override
            public void onSuccess(Bitmap bmp) {
                if (bmp == null) {
                    mImageView.setScaleType(ScaleType.CENTER);
                    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.def_logo);
                    mImageView.setImageBitmap(bmp);
                } else {
                    mImageView.setScaleType(ScaleType.MATRIX);
                    mImageView.setImageBitmap(bmp);
                }
                mImageView.setVisibility(VISIBLE);
                circleProgressBar.setVisibility(GONE);
            }

            @Override
            public void onFailure() {
                mImageView.setScaleType(ScaleType.CENTER);
                Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.def_logo);
                mImageView.setImageBitmap(bmp);
                mImageView.setVisibility(VISIBLE);
                circleProgressBar.setVisibility(GONE);
            }
        });
    }

    public interface OnSaveCallBack {
        void onSaveCallBack(Bitmap bmp);
    }

    public interface OnClickCallBack {
        void onClickCallBack();
    }
}
